package com.yj.ccs.abs.web.common.mapper;

import com.yj.ccs.abs.web.common.entity.Attachment;
import com.yj.ccs.abs.web.management.dto.IdListDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/21
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface AttachmentMapper {

    @Insert("<script>" +
            "INSERT INTO `attachment` (reference_id, url, `name`, type, category, client_uid) VALUES " +
            "<foreach collection = 'list' item = 'item' separator = ', '>" +
            "(#{item.reference_id}, #{item.url}, #{item.name}, #{item.type}, #{item.category}, #{item.client_uid})" +
            "</foreach>" +
            "</script>")
    void batchCreate(@Param("list") List<Attachment> list);

    @Delete("DELETE FROM `attachment` WHERE reference_id = #{id} AND category = #{type}")
    void deleteByIdAndType(@Param("id") Long id, @Param("type") Integer type);

    @Delete("<script>" +
            "DELETE FROM `attachment` " +
            "WHERE reference_id IN ( " +
            "<foreach collection = 'item.id_list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ") " +
            "AND category = #{type}" +
            "</script>")
    void batchDeleteByIdsAndType(@Param("item") IdListDto item, @Param("type") Integer type);

    @Select("SELECT * FROM `attachment` WHERE reference_id  = #{id} AND category = #{type}")
    List<Attachment> getByIdAndType(@Param("id") Long id, @Param("type") Integer type);
}
